package com.dankegongyu.shell.js;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.dankegongyu.component.jsbridge.JsBridgeConstants;
import com.dankegongyu.component.jsbridge.config.DKWebConfig;
import com.dankegongyu.component.jsbridge.config.DKWebService;
import com.dankegongyu.component.jsbridge.config.DKWebServiceManager;
import com.dankegongyu.component.jsbridge.config.DKWebToolbarConfig;
import com.dankegongyu.component.jsbridge.sdk.JsRouter;
import com.dankegongyu.shell.js.common.IOnItemClickListener;
import com.dankegongyu.shell.js.common.ShellData;
import com.dankegongyu.shell.js.common.SimpleRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class ShellActivity extends Activity {

    private TextView toolbar;
    private RecyclerView recyclerView;

    private List<ShellData> mData = new ArrayList();

    private Activity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mActivity = this;

        toolbar = findViewById(R.id.tv_title);
        recyclerView = findViewById(R.id.recycle_view);

        toolbar.setText("JsBridge模块调试");

        LinearLayoutManager horAnalyse = new LinearLayoutManager(this);
        horAnalyse.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(horAnalyse);

        initData();
        SimpleRecycleAdapter adapter = new SimpleRecycleAdapter(this, mData, R.layout.cell_click_item);
        adapter.setOnItemClickListener(mIOnItemClickListener);
        recyclerView.setAdapter(adapter);

    }


    private IOnItemClickListener mIOnItemClickListener = new IOnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {

            if (position == 0) {
                String serviceKey = String.valueOf(System.currentTimeMillis());
                DKWebService service = DKWebServiceManager.getInstance().get(serviceKey);

                DKWebToolbarConfig toolbarConfig = new DKWebToolbarConfig.Builder()
                        .setToolbarVisible(true)
                        .setToolbarColorRes(R.color.color_0000ff)
                        .setStatusbarColor(R.color.color_00ff00)
                        .setStatusbarAlpha(128)
                        .setStatusbarMode(JsBridgeConstants.Statusbar.STATUSBAR_LIGHT)

                        .setTitle("必读")
                        .setTitleColor(R.color.color_ff0000)
                        .setTitleSize(20)

                        .setBottomLineVisible(true)
//                        .setBottomLineDrawableRes(R.drawable.toolbar_line_shade)

                        .setBackBtnVisible(true)
                        .setBackBtnRes(R.drawable.jbp_back)

                        .setCloseBtnVisible(true)
                        .setCloseBtnRes(R.drawable.jbp_close)

                        .builder();
                String url = "https://www.baidu.com";
                DKWebConfig config = new DKWebConfig.Builder()//
                        .setToolbar(toolbarConfig)
                        .setClientCallback(new DKWebClient(serviceKey))
                        .setUrl(url)
                        .builder();
                service.register(config);

                JsRouter.open(mActivity, serviceKey);
            }

        }
    };

    /**
     * 添加名称与className即可。
     */
    private void initData() {
        mData.add(new ShellData("首页", ""));
    }


}
